<?php
	//获取当前模板名称
	function getStyleName(){
		global $mysql;
		$res = $mysql->dbSelect('config',"name=?",array('style'));
		return $res[0]['value'];
	}
	//读取博客配置项
	function getConfig($name=0){
		global $mysql;
		if(!is_string($name)){
			$res = $mysql->dbSelect('config');
			return $res;
		}else{
			$res = $mysql->dbSelect('config','name=?',array($name));
			return $res[0]['value'];
		}
	}
	//登录验证
	function loginSub($username,$password,$isadmin=0){
		global $mysql;
		$md5 = $password;
		$res = $mysql->dbSelect('user','username=?',array($username));
		if($isadmin==1){
			if($res[0]['isadmin']!=1){
				return false;
			}
		}
		if($md5==$res[0]['password']){
			return true;
		}else{
			return false;
		}
	}
	//登录验证(id版)
	function loginSubById($id,$password,$isadmin=0){
		global $mysql;
		$md5 = $password;
		$res = $mysql->dbSelect('user','uid=?',array($id));
		if($isadmin==1){
			if($res[0]['isadmin']!=1){
				return false;
			}
		}
		if($md5==$res[0]['password']){
			return true;
		}else{
			return false;
		}
	}
	//获取用户数据
	function getUserById($id){
		global $mysql;
		$res = $mysql->dbSelect('user','uid=?',array($id));
		return $res[0];
	}
	function getUserList(){
		global $mysql;
		$res = $mysql->dbSelect('user','1','');
		return $res;		
	}
	//获取用户数据
	function getUserByName($username){
		global $mysql;
		$res = $mysql->dbSelect('user','username=?',array($username));
		return $res[0];		
	}
	//获取博文总数
	function getArticleNum($kindid=-1){
		global $mysql;
		if($kindid==-1){
			$res = $mysql->dbSelect('article','',0,1);
		}else{
			$res = $mysql->dbSelect('article','kindid=?',array($kindid),1);
		}
		
		return $res[0][0];
	}
	//获取类型总数
	function getKindNum(){
		global $mysql;
		$res = $mysql->dbSelect('artkind','',0,1);
		return $res[0][0];		
	}
	//获取评论总数
	function getRemarkNum(){
		global $mysql;
		$res = $mysql->dbSelect('remark','',0,1);
		return $res[0][0];				
	}
	//获取博文浏览量
	function getPageView(){
		global $mysql;
		$res = $mysql->dbSelect('remark','',0,1);
		return $res[0][0];			
	}
	//获取用户总数
	function getUserNum(){
		global $mysql;
		$res = $mysql->dbSelect('user','',0,1);
		return $res[0][0];			
	}
	//获取mysql版本
	function getMysqlVertion(){
		global $mysql;
		$res = $mysql->query('select VERSION()');
		return $res[0][0];
	}
	function getMysqlSize(){
		global $mysql;
		$res = $mysql->query("SELECT sum(DATA_LENGTH)+sum(INDEX_LENGTH)FROM information_schema.TABLES where TABLE_SCHEMA='moonblog'");
		$size = $res[0][0];
		if((int)($size/(1024*1024))!=0)
			$size = $size/(1024*1024)."M";
		elseif((int)($size/(1024))!=0)
			$size = (int)($size/(1024))."K";
		else
			$size = (int)($size)."byte";
		return $size;
	}
	//获取友链
	function getLink(){
		global $mysql;
		$res = $mysql->dbSelect('link','1 ORDER BY `index`','');
		return $res;
	}
	//提交友链
	function subLink($index,$name,$url,$headpic,$introduce,$color){
		global $mysql;
		if($color!=''){
			$mysql->dbInsert('link',array('index','name','url','headpic','introduce','color'),array($index,$name,$url,$headpic,$introduce,$color));
		}else{
			$mysql->dbInsert('link',array('index','name','url','headpic','introduce'),array($index,$name,$url,$headpic,$introduce));
		}
		return true;
	}
	//删除友链
	function deleteLink($id){
		global $mysql;
		$mysql->dbDelete('link','id=?',array($id));
		return true;
	}
	//更新友链
	function updateLink($id,$data){
		global $mysql;
		$key = array('index','name','url','headpic','introduce','color');
		$mysql->dbUpdate('link',$key,$data,'id=?',array($id));
		return true;
	}
	//发布文章
	function subArticle($title,$content,$artkind,$state,$author,$time,$istop,$isbanr,$titleimg,$isshow,$ispage,$isupdate=0,$id=null){
		if(!isset($title)||!is_string($title)||strlen($title)>100){
			return false;
		}elseif(!isset($content)||!is_string($content)){
			return false;
		}elseif(!isset($author)||!is_string($author)||strlen($author)>50){
			return false;
		}elseif(!isset($artkind)||!isset($state)||!isset($isbanr)||!isset($istop)||!isset($time)){
			return false;
		}

		global $mysql;
		if($isupdate==0){
			$key = array('title','content',"kindid","state","author","pubtime","istop","isbanr","titleimg","isshow","ispage");
			$arr = array($title,$content,$artkind,$state,$author,$time,$istop,$isbanr,$titleimg,$isshow,$ispage);
			$mysql->dbInsert('article',$key,$arr);			
		}
		elseif($isupdate==1){
			$key = array('title','content',"kindid","state","author","istop","isbanr","titleimg","isshow","ispage");
			$arr = array($title,$content,$artkind,$state,$author,$istop,$isbanr,$titleimg,$isshow,$ispage);
			$mysql->dbUpdate('article',$key,$arr,'id=?',array($id));			
		}
		return true;
	}

	function subRemark($artid,$authorid,$name,$content,$pubtime,$email,$url){
		if(($authorid==0&&strlen($name)<1)||strlen($content)<1||strlen($content)>500){
			return false;
		}
		$content = dealText($content);
		
		global $mysql;
		if($authorid==0){
			$key = array('name','artid','authorid','content','pubtime','email','url');
			$arr = array($name,$artid,$authorid,$content,$pubtime,$email,$url);			
		}else{
			$key = array('artid','authorid','content','pubtime');
			$arr = array($artid,$authorid,$content,$pubtime);			
		}
		$mysql->dbInsert('remark',$key,$arr);
		$mysql->query("UPDATE article set remarknum=remarknum+1 WHERE id={$artid}");

		return true;
	}
	//通过文章ID获取相应评论
	function getRemarkByArtid($artid,$s=0,$e=0,$iscount=0){
		global $mysql;
		if($iscount==0){
			$res = $mysql->dbSelect('remark','artid=? ORDER BY id DESC LIMIT ?,?',array($artid,$s,$e));
			return $res;
		}else{
			$res = $mysql->dbSelect('remark','artid=? ORDER BY id DESC',array($artid),$iscount);
			return $res[0][0];
		}
	}
	function unsetArticle($id){
		global $mysql;	
		$mysql->dbDelete('article','id=?',array($id));
		$mysql->dbDelete('remark','artid=?',array($id));
		return true;
	}
	function getArticle($id){
		global $mysql;
		$res = $mysql->dbSelect('article','id=?',array($id));
		return $res[0];
	}
	//获取分类名称
	function getArtkind($id){
		global $mysql;
		$res = $mysql->dbSelect('artkind','id=?',array($id));
		return $res[0]['name'];		
	}
	//增加分类
	function addArtkind($name){
		global $mysql;
		$mysql->dbInsert("artkind",array('name'),array($name));
		return true;
	}
	//删除分类
	function deleteArtKind($id){
		global $mysql;
		$mysql->dbDelete("artkind",'id=?',array($id));
		$mysql->dbUpdate('article',array('kindid'),array(0),'kindid=?',array($id));
		return true;		
	}
	//更新分类
	function updateArtKind($id,$name){
		global $mysql;
		$mysql->dbUpdate("artkind",array('name'),array($name),'id=?',array($id));
		return true;
	}
	function getAirtkindList(){
		global $mysql;
		$res = $mysql->dbSelect('artkind');
		return $res;
	}
	function turnTimeToDate($time,$style=1){
		if($style==1)
			return date("Y年m月d日",$time);
		elseif($style==2)
			return date("Y-m-d",$time);
		elseif($style==3)
			return date("Y年m月d日  H:i",$time);
	}
	//删除标签
	function dealText($text){
		$text = htmlentities($text);
		return $text;
	}
	//获取置顶文章列表
	function getTopArticle(){
		global $mysql;
		$res = $mysql->dbSelect('article','state=? and istop=1 ORDER BY pubtime DESC',array(0));
		foreach($res as $key=>$value){
			$res[$key]['pubtime'] = turnTimeToDate($value['pubtime']);
			$res[$key]['content'] = (cn_substr(strip_tags($res[$key]['content']),0,400)).'...';
		}
		return $res;
	}
	//获取前台文章列表
	function getArticleList($start,$end,$artkind=-1){
		global $mysql;
		if($artkind==-1){
			$res = $mysql->dbSelect('article','state=? and isshow=1 ORDER BY pubtime DESC LIMIT ?,?',array(0,$start,$end));
		}else{
			$res = $mysql->dbSelect('article','state=? and kindid=? ORDER BY pubtime DESC LIMIT ?,?',array(0,$artkind,$start,$end));
		}
		
		foreach($res as $key=>$value){
			$res[$key]['pubtime'] = turnTimeToDate($value['pubtime']);
			$res[$key]['content'] = (cn_substr(strip_tags($res[$key]['content']),0,400)).'...';
		}
		
		return $res;
	}
	//获取后台文章列表
	function getArticleAllList($start,$end,$artkind=-1){
		global $mysql;
		if($artkind==-1){
			$res = $mysql->dbSelect('article','1 ORDER BY id DESC',array());
		}else{
			$res = $mysql->dbSelect('article','1 kindid=? ORDER BY id DESC LIMIT ?,?',array($artkind,$start,$end));
		}
		
		foreach($res as $key=>$value){
			$res[$key]['pubtime'] = turnTimeToDate($value['pubtime']);
			$res[$key]['content'] = strip_tags(cn_substr($res[$key]['content'],0,300)).'...';
			
		}
		
		return $res;
	}
	//中文字符串截取(防止出现切割汉字)
	function cn_substr($str,$start,$end){
		$strlen = $end - $start;
		$tmpstr = '';
		for($i=0;$i<$strlen;$i++){ 
            if(ord(substr($str,$i,1))>0xa0){ 
                $tmpstr.=substr($str,$i,3);        
                $i+=2;
            }else{                                             
                $tmpstr.=substr($str,$i,1);
            }

        }
        return $tmpstr;
	}
	function addArticleView($id){
		global $mysql;
		$mysql->pdo->exec("UPDATE `article` SET view=view+1 WHERE id={$id}");
	}
	function deleteRemark($id,$artid){
		global $mysql;
		$count = $mysql->dbSelect('remark','id=?',array($id));
		if(count($count)!=0){
			$res = $mysql->dbDelete('remark','id=?',array($id));
			$mysql->query("UPDATE article set remarknum=remarknum-1 WHERE id={$artid}");			
		}
		return true;
	}
	function getRemarkList($num){
		global $mysql;
		$res = $mysql->dbSelect('remark','1 ORDER BY id DESC LIMIT 0,?',array($num),0);
		return $res;
	}
	function updateImgNumber($num){
		global $mysql;
		$res = $mysql->dbSelect('config','name=?',array('imgnum'));
		if(count($res)==0){
			$mysql->dbInsert('config',array('name','value'),array('imgnum','0'));
		}
		$mysql->query("UPDATE config set value=value+{$num} WHERE name='imgnum'");
	}
	function getImgNumber(){
		global $mysql;
		$res = $mysql->dbSelect('config','name=?',array('imgnum'));
		return (int)$res[0]['value'];
	}
	function checkFile($file){
		/*
		0:通过检测
		1:文件类型不符合
		2:文件大小超限
		*/
		foreach($file as $value){
			if($value['type']!='image/bmp'&&$value['type']!='image/png'&&$value['type']!='image/gif'&&$value['type']!='image/jpeg'){
				return 1;
			}elseif($value['size']>2000000){
				return 2;
			}
		}
		return 0;
	}
	function updateSettings($name,$value){
		global $mysql;
		$res = $mysql->dbSelect('config','name=?',array($name));
		if(count($res)==0){
			$mysql->dbInsert('config',array('name','value'),array($name,$value));
		}else{
			$mysql->dbUpdate('config',array('value'),array($value),'name=?',array($name));
		}
	}
	//读取topMenu
	function getTopMenuData(){
		global $mysql;
		$res = $mysql->dbSelect('topmenu','1 ORDER BY `index`','');
		return $res;
	}
	//删除topMenu
	function deleteTopMenu($id){
		global $mysql;
		$mysql->dbDelete('topmenu','id=?',array($id));
		return true;
	}
	//增加topMenu
	function addOneMenu($data){
		global $mysql;
		$already = getTopMenuData();
		$already = count($already);
		$id=0;
		foreach($data as $value){
			if(strlen($value[0])==0||strlen($value[1])==0||strlen($value[2])==0){
				continue;
			}
			if($id<$already){
				$mysql->dbUpdate('topmenu',array('index','title','url','state'),array((int)$value[0],$value[1],$value[2],(int)$value[3]),'id=?',array((int)$value[4]));
			}else{
				$mysql->dbInsert('topmenu',array('index','title','url','state'),array((int)$value[0],$value[1],$value[2],(int)$value[3]));

			}
			$id++;
		}
		return true;
	}
	function addUser($data){
		global $mysql;
		$mysql->dbInsert('user',array('username','password','qq','isadmin','regtime'),$data);
		return true;
	}
	function updateUserDate($userid,$key,$value){
		global $mysql;
		$mysql->dbUpdate('user',$key,$value,"uid=?",array($userid));
		return true;
	}
	//检测注册提交数据
	function checkUserData($data){
		if(preg_match("/^[\w\x{4e00}-\x{9fa5}]{2,16}$/u",$data['username'])!=true){
			return 2;
		}elseif(preg_match("/^[\S]{6,16}$/",$data['password'])!=true){
			return 3;
		}elseif($data['qq']!=null&&preg_match("/^[\d]{5,12}$/",$data['qq'])!=true){
			return 4;
		}
		global $mysql;
		$res = $mysql->dbSelect('user','username=?',array($data['username']));
		if(count($res)!=0){
			return 5;
		}

		return 1;
	}
	//检测修改信息数据
	function checkUpdateData($data){
		if(strlen($data['password'])>0&&preg_match("/^[\S]{6,16}$/",$data['password'])!=true){
			return 3;
		}elseif(strlen($data['qq'])>0&&preg_match("/^[\d]{5,12}$/",$data['qq'])!=true){
			return 4;
		}		
		foreach($data as $value){
			if(strlen($value)>50){
				return 6;
			}
		}
		return 1;		
	}
	//检测是否有该用户
	function checkHasName($name){
		global $mysql;
		$res = $mysql->dbSelect('user','username=?',array($name));
		if(count($res)==0){
			return true;
		}else{
			return false;
		}
	}
	function extractImageSizeAndResponse($publicPathImage) 
	{ 
		// getimagesize() 如果不存在或非图片文件会返回 false 并产生 E_WARNING 级错误 
		$imageSize = getimagesize($publicPathImage); 
		if ($imageSize == false) { 
			return false; 
		} 
		switch ($imageSize[2]) { 
			case 1: // IMAGETYPE_GIF 
				$imageSize[3] = imagecreatefromgif($publicPathImage); 
				break; 
			case 2: // IMAGETYPE_JPEG 
				$imageSize[3] = imagecreatefromjpeg($publicPathImage); 
				break; 
			case 3: // IMAGETYPE_PNG 
				$imageSize[3] = imagecreatefrompng($publicPathImage); 
				break; 
			default: 
				return false; 
		} 
		return $imageSize; 
	}
	
?>